package com.sw.test01.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sw.test01.R;
import com.sw.test01.domain.bean.Person;

import java.util.List;

/**
 * Created by xifanoo on 2017/3/22.
 */

public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {

    private static final String TAG = "PersonAdapter";
    private List<Person> mList;

    public PersonAdapter(List<Person> mList) {
        this.mList = mList;
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_person,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Log.w(TAG, "onClick: img"+position);
            }
        });
        holder.personView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Log.d(TAG, "onClick: personView" + position);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Person person = mList.get(position);
        holder.img.setImageResource(person.getImgId());
        holder.tvName.setText(person.getName());
        holder.tvAge.setText(person.getAge());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView tvName;
        TextView tvAge;
        View personView;
        public ViewHolder(View view) {
            super(view);
            personView = view;
            img = (ImageView) view.findViewById(R.id.img);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvAge = (TextView) view.findViewById(R.id.tv_age);
        }
    }
}
